'use strict';
const dayjs = require("dayjs");

module.exports = app => {
    const { STRING, INTEGER, DATE, BIGINT, BOOLEAN, DECIMAL, JSON } = app.Sequelize;
    const ProductApplication = app.model.define(
        'ProductApplication',
        {
            id: {
                type: BIGINT.UNSIGNED,
                primaryKey: true,
                autoIncrement: true,
                comment: '主键ID'
            },
            user_id: {
                type: BIGINT.UNSIGNED,
                allowNull: false,
                comment: '用户ID'
            },
            product_id: {
                type: BIGINT.UNSIGNED,
                allowNull: false,
                comment: '申请产品ID'
            },
            contact_person: {
                type: STRING(50),
                allowNull: false,
                comment: '联系人'
            },
            contact_phone: {
                type: STRING(20),
                allowNull: false,
                comment: '联系方式'
            },
            social_security_base: {
                type: DECIMAL(10, 2),
                allowNull: true,
                comment: '社保缴纳基数'
            },
            housing_fund_base: {
                type: DECIMAL(10, 2),
                allowNull: true,
                comment: '公积金缴纳基数'
            },
            salary_range: {
                type: STRING(50),
                allowNull: true,
                comment: '工资区间'
            },
            application_data: {
                type: JSON,
                allowNull: false,
                comment: '申请填报数据'
            },
            review_status: {
                type: INTEGER,
                allowNull: false,
                defaultValue: 1,
                comment: '审核状态(1-待审核,2-审核通过,3-审核拒绝)'
            },
            review_remark: {
                type: STRING(500),
                allowNull: true,
                comment: '审核备注'
            },
            assignee: {
                type: STRING(64),
                allowNull: true,
                comment: '分配的业务员'
            },
            creator: {
                type: STRING(64),
                allowNull: false,
                defaultValue: '',
                comment: '创建者'
            },
            create_time: {
                type: DATE,
                allowNull: false,
                defaultValue: DATE.NOW,
                comment: '申请时间',
                get() {
                    return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            },
            updater: {
                type: STRING(64),
                allowNull: false,
                defaultValue: '',
                comment: '更新者'
            },
            update_time: {
                type: DATE,
                allowNull: false,
                defaultValue: DATE.NOW,
                comment: '更新时间',
                get() {
                    return dayjs(this.getDataValue('update_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            },
            deleted: {
                type: BOOLEAN,
                allowNull: false,
                defaultValue: false,
                comment: '是否删除'
            }
        },
        {
            tableName: 'product_application',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time',
            indexes: [
                {
                    name: 'idx_user_id',
                    fields: ['user_id']
                },
                {
                    name: 'idx_product_id',
                    fields: ['product_id']
                },
                {
                    name: 'idx_review_status',
                    fields: ['review_status']
                },
                {
                    name: 'idx_assignee',
                    fields: ['assignee']
                },
                {
                    name: 'idx_create_time',
                    fields: ['create_time']
                },
                {
                    name: 'idx_deleted',
                    fields: ['deleted']
                }
            ]
        }
    );

    // 关联产品用户
    ProductApplication.associate = function () {
        // 关联产品分类
        app.model.ProductApplication.belongsTo(app.model.Product, {
            foreignKey: 'product_id',
            as: 'product'
        });
        // 关联用户
        app.model.ProductApplication.belongsTo(app.model.User, {
            foreignKey: 'user_id',
            as: 'user'
        });
    }
    return ProductApplication
}; 